.TH std::get(std::variant) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::get(std::variant) \- std::get(std::variant)

.SH Synopsis
   Defined in header <variant>
   template< std::size_t I, class... Types >

   constexpr std::variant_alternative_t<I,
   std::variant<Types...>>&

       get( std::variant<Types...>& v );
   template< std::size_t I, class... Types >

   constexpr std::variant_alternative_t<I,
   std::variant<Types...>>&&

       get( std::variant<Types...>&& v );
   template< std::size_t I, class... Types >

   constexpr const std::variant_alternative_t<I,
   std::variant<Types...>>&
                                                         \fB(1)\fP \fI(since C++17)\fP
       get( const std::variant<Types...>& v );
   template< std::size_t I, class... Types >

   constexpr const std::variant_alternative_t<I,
   std::variant<Types...>>&&

       get( const std::variant<Types...>&& v );
   template< class T, class... Types >
   constexpr T& get( std::variant<Types...>& v );
   template< class T, class... Types >
   constexpr T&& get( std::variant<Types...>&& v );
   template< class T, class... Types >                                     \fB(2)\fP (since
   constexpr const T& get( const std::variant<Types...>&                       C++17)
   v );
   template< class T, class... Types >
   constexpr const T&& get( const
   std::variant<Types...>&& v );

   1) Index-based value accessor: If v.index() == I, returns a reference to the value
   stored in v. Otherwise, throws std::bad_variant_access. The call is ill-formed if I
   is not a valid index in the variant.
   2) Type-based value accessor: If v holds the alternative T, returns a reference to
   the value stored in v. Otherwise, throws std::bad_variant_access. The call is
   ill-formed if T is not a unique element of Types....

.SH Template parameters

   I        - index to look up
   T        - unique type to look up
   Types... - types forming the variant

.SH Parameters

   v - a variant

.SH Return value

   Reference to the value stored in the variant.

.SH Exceptions

   1,2) Throws std::bad_variant_access on errors.

.SH Example


// Run this code

 #include <variant>
 #include <string>
 #include <iostream>

 int main()
 {
     std::variant<int, float> v{12}, w;
     std::cout << std::get<int>(v) << '\\n';
     w = std::get<int>(v);
     w = std::get<0>(v); // same effect as the previous line

 //  std::get<double>(v); // error: no double in [int, float]
 //  std::get<3>(v);      // error: valid index values are 0 and 1

     try
     {
         w = 42.0f;
         std::cout << std::get<float>(w) << '\\n'; // ok, prints 42
         w = 42;
         std::cout << std::get<float>(w) << '\\n'; // throws
     }
     catch (std::bad_variant_access const& ex)
     {
         std::cout << ex.what() << ": w contained int, not float\\n";
     }
 }

.SH Possible output:

 12
 42
 Unexpected index: w contained int, not float

.SH See also

                              obtains a pointer to the value of a pointed-to variant
   get_if                     given the index or the type (if unique), returns null on
   \fI(C++17)\fP                    error
                              \fI(function template)\fP
   get(std::tuple)            tuple accesses specified element
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::array)            accesses an element of an array
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::pair)             accesses an element of a pair
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::ranges::subrange) obtains iterator or sentinel from a std::ranges::subrange
   (C++20)                    \fI(function template)\fP

.SH Category:
     * Uses of dcl rev begin with nonempty note
